package com.gyx.metaldesigndemo.recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyx.metaldesigndemo.R;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Administrator on 2016/11/15.
 */

public class RecyclerViewDraggedAdapter extends RecyclerView.Adapter<RecyclerViewDraggedAdapter.MyHolder>
    implements ItemTouchHelperAdapter
{

    private Context context;

    private ArrayList<String> list;

    public RecyclerViewDraggedAdapter(Context context){
        this.context=context;

        list=new ArrayList<>();
        for (int i=0 ; i<50 ;i++){
            list.add("item-"+i);
        }
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_view,parent,false));
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
          holder.textView.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(list,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }

    @Override
    public void onItemDismiss(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }


    public  static class MyHolder extends RecyclerView.ViewHolder{

        TextView textView;
        public MyHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.tv1);
        }
    }



}
